package com.cinemtl.manager;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.cinemtl.dbConnection.DbConnection;
import com.cinemtl.beans.Order;

public class OrderManager {

	public static Order getById(int id) {
		Order order = new Order();
		String query = "SELECT * FROM order WHERE id = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setInt(1, id);

			ResultSet result = ps.executeQuery();

			while (result.next()) {
				order.setId(result.getInt("id"));
				order.setIdUser(result.getInt("id_user"));
				order.setIdMovie(result.getInt("id_movie"));
				order.setPurchaseDate(result.getDate("purchase_date"));
				order.setNbTicket(result.getInt("qty"));
				
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

		return order;
	}

	public static ArrayList<Order> getByIdUser(int idUser) {
		ArrayList<Order> orders = new ArrayList<Order> ();
		String query = "SELECT * FROM order WHERE id_user = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setInt(1, idUser);

			ResultSet result = ps.executeQuery();

			while (result.next()) {
				Order order = new Order();
				order.setId(result.getInt("id"));
				order.setIdUser(result.getInt("id_user"));
				order.setIdMovie(result.getInt("id_movie"));
				order.setPurchaseDate(result.getDate("purchase_date"));
				order.setNbTicket(result.getInt("qty"));
				orders.add(order);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

		return orders;
	}
	public static ArrayList<Order> showOrder(int idUser) {
		ArrayList<Order> orders = new ArrayList<Order> ();
		String query = "SELECT order.id,movie.title, movie.url_image"
				+ " FROM order as o INNER JOIN  movie as m ON o.id_movie = m.id WHERE order.id_user = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setInt(1, idUser);

			ResultSet result = ps.executeQuery();

			while (result.next()) {
				Order order = new Order();
				order.setId(result.getInt("id"));
				order.setIdUser(result.getInt("id_user"));
				order.setIdMovie(result.getInt("id_movie"));
				order.setPurchaseDate(result.getDate("purchase_date"));
				order.setNbTicket(result.getInt("qty"));
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
		return orders;
	}
	public static void insert(Order order) {
		String query = "INSERT INTO `order` (id_user,id_movie,qty) VALUES (?,?,?)";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setInt(1, order.getIdUser());
			ps.setInt(2, order.getIdMovie());
			ps.setInt(3, order.getNbTicket());
			ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

	}
}
